package com.dianban.utils.http;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class RSAUtil {

    /**
     * RSA公钥加密
     * @param str 加密字符串
     * @param publicKey 公钥
     * @return 密文
     * @throws Exception 加密过程中的异常信息
     */
    public static String encrypt(String str, String publicKey) throws Exception{//base64编码的公钥
        byte[] decoded =Base64.decodeBase64(publicKey);
        RSAPublicKey pubKey= (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));//RSA加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        String outStr= Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
        return outStr;
    }

    /**
     * RSA私钥解密
     * @param str 加密字符串
     * @param privateKey 私钥
     * @return 明文
     * @throws Exception 加密过程中的异常信息
     */
    public static String decrypt(String str, String privateKey) throws Exception {//64位解码加密后的字符串
        byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));//base64编码的私钥
        byte[] decoded =Base64.decodeBase64(privateKey);
        RSAPrivateKey priKey= (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));//RSA解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        String outStr= new String(cipher.doFinal(inputByte));
        return outStr;
    }

    public static void main(String[] args) {
        try {
            String publicKey= "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCSLTe3d+kab964ZZjrtleSEbF4Oz3m8hUnLECjI3powM4HJK8ly4SBa8txXKdNm40QCZYX7WJzuXG1lSJ2ABEuiI7kKIaWaLB+vJJoYPaaCNDpNHTpzqBvsyLoIchbcLKIoLp2/Pf0DoF82b/xNn6hDcujAZ5B3ZqAzqpompAWBwIDAQAB";
            String encrypt = RSAUtil.encrypt("{\"timestamp\":1596855124,\"appid\":\"xxxxx\"}",publicKey);
            System.out.println("encrypt:"+encrypt);
            String privateKey = "MIICXQIBAAKBgQCSLTe3d+kab964ZZjrtleSEbF4Oz3m8hUnLECjI3powM4HJK8ly4SBa8txXKdNm40QCZYX7WJzuXG1lSJ2ABEuiI7kKIaWaLB+vJJoYPaaCNDpNHTpzqBvsyLoIchbcLKIoLp2/Pf0DoF82b/xNn6hDcujAZ5B3ZqAzqpompAWBwIDAQABAoGADEmaxmRhBuTifvOjYCUKxW+RUH8CXBh7aLiSkmQYFRgn+Xe+9VlfQRXTmyrjsWVi/TchE8/Tl9C92zq3E947k2H57uB+dVvbVKSt2Na/9V7ezRVZw6Jf57LBlqWKjWa6TfNPMlHdjdJfKzkagBHH6Ge+5S5y08NgUkSoHKl8G4ECQQDdumGevfREElQuucUAVNPWCdhEzrUZWl54tY18a3Q3Wt2pM9aGyULgFU0v6bAaupntzHmliOqklhWicUkmzp5BAkEAqMVSPJUbCrPMOIFwAEOHVUvgvYQNM+C20IKSZzlbVon2c3/cCFWl5IljRz3fgnbVX1IQ/maPNXtNzhrbQPmyRwJBANG7JfHIzg/Bop1cHSs2EfaI82gKeqUTpP7bBI0m+xQNQFR3czmlkDt0CsICcxClZ0rXOk3JV+H9EVPJgp9Rm8ECQGG3RcMP7cVHjtGUl/1/G8dWFHyGDYLg89hMEWo0ul6jqI79E6+7rz5gEZw5BcUq+qWjBXXWecpUZxsTT6kSKZcCQQCz69qBl2wmZzr2vX2XrCTh4uVXS0KoJgbox5o3C1cLsNEpPKHs+u7H6CA1ifylbuoEaP9Y5z4/MG5+6s10gVlW";
            String decrypt = RSAUtil.decrypt(encrypt,privateKey);
            System.out.println("decrypt:"+decrypt);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
